package com.ruoyi.system.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;

public class AlipayTradeWapPayUtils {

    public static String getAliPayResBody(String outTradeNo,String totalAmount,String subject) throws AlipayApiException{

        String privateKey = AlipayConstants.applicationPrivateKey;
        String alipayPublicKey = AlipayConstants.aliPublicKey;
        String appId = AlipayConstants.alipayAppId;
        String serverUrl = AlipayConstants.alipayServerUrl;
        String sellerId = AlipayConstants.sellerId;

        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(serverUrl);
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();

        request.setNotifyUrl(AlipayConstants.alipayNotifyUrl);
        request.setReturnUrl(AlipayConstants.alipayReturnUrl);

        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(outTradeNo);
        model.setTotalAmount(totalAmount);
        model.setSubject(subject);
        model.setProductCode("QUICK_WAP_WAY");
        model.setSellerId(sellerId);
        request.setBizModel(model);
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");
        String result;
        if (response.isSuccess()) {
            result = response.getBody();
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
            result = "error";
        }
        return result;
    }

    public static String getTestDemo2() throws AlipayApiException{
        String privateKey = "MIIEvwIBADANBgk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CgYEAgWkZWd0QgvO4I4/eX8qQXvmFfYehs4Ribtd8sqDHZfMv4ZJelEmRHzSkK4W6dm5rNuYIRUbZHDKKYg8GGNBCDKwy9jOJBz9R//8rTBxPe6p67EizVhtDEkUpbq2yjTfm6yCpJzlFMZ1mEa/r9TtKZNdstjG5z4IfP9mm8ueCl2MCgYEAnf/8hyF5Gxv0Ig/PaVdGT9ozEv7Ov0QYgjLghKb3ydAlaIhwDSjFn++EhQSXYcdujpRhqwjAk3F86aRfyJ7Ux9D+6SYpoI8dITSTY5Br42X1KIRm454uSNjBCBdURxmdQXt9RIQ2IEpVpyTApU0uACes2UUleYzG6d//q12JIzECgYAJ+aqkxlfSTaos43Gwls8pjb3yrp06lpYJyw3PcEdGUPbTiWVsZOKWDyjZuNUQ0gqFj6N2EGZ6o9LcWsng3PVKhoSg4ibHam0/IcMnkdZ20MYcGMIxxeE1m5SUMe0dcN2r+qUpXLrMwY2JbG+JNlCnwUBMOhLinCux1PjKW7f5UQ==";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlaPoXXaodaqJ4dqi1SDv4wpj1VdxEDteB5g6Sl+g7+bbZ4yx0TIqs3U7FWsV2DS6vP9a48AYYR4U/sIsTEeIR0HK1o20/nPQszHDb5ntiUrLdaAEcUSSltRddfXUB86fA+Sm9arPb/OjEExtDAHFC3/CBjhU8oAgR7HE2tFgEgEseWT/8cp76xth7kes6aWok6RmbNQpgsZ6WMyUuPsW8S0EtSW0OYO1oIr5zZ9wM6X/75ocMWfbOSXpNG6dVSmqbLqI7kK0tnjN13KGC9oPZPLVurtCYyzFRG55d3/3zFvcK79gvSZjwY/816vEICD6YRNqxiror0Jvk0gE640iAwIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("2021004155618161");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo("70501111111S001111119");
        model.setTotalAmount("119.00");
        model.setSubject("大乐透");
        model.setProductCode("QUICK_WAP_WAY");
        model.setSellerId("2088840667932881");
        request.setBizModel(model);
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        // AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return pageRedirectionData;
    }

    public static void main(String[] args) {
        String outTradeNo = "70501111111S001111119";
        String totalAmount = "9.00";
        String productCode = "QUICK_WAP_WAY";
        try{
            String str = getAliPayResBody(outTradeNo,totalAmount,"支付标题");
        } catch (Exception e){
            System.out.println(e);
        }
    }
}